package com.shouer.doc.ui.userinfo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.shouer.doc.R;
import com.shouer.doc.application.App;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.FileUtil;
import com.shouer.doc.dialog.GetCashApplyDialogUtil;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.ui.LasyFragment;
import com.shouer.doc.ui.billlist.BillListActivity;
import com.shouer.doc.ui.login.model.UserModel;
import com.shouer.doc.ui.main.MainActivity;
import com.shouer.doc.ui.modifypass.ModifyPassActivity;
import com.shouer.doc.ui.qrcode.QRCodeActivity;
import com.shouer.doc.ui.userinfo.adapter.BillAdapter;
import com.shouer.doc.ui.userinfo.model.BillModel;
import com.shouer.doc.ui.userinfo.presenter.IUserInfoPresenter;
import com.shouer.doc.ui.userinfo.presenter.UserInfoPresenterCompl;
import com.shouer.doc.ui.userinfo.view.IUserInfoView;
import com.shouer.doc.widget.ShouerLayoutManager;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZXL on 2016/11/25.
 */

public class UserInfoFragment extends LasyFragment implements View.OnClickListener,IUserInfoView{

    private App app;

    private View mRootView;
    private boolean isPrepared;
    @BindView(R.id.bill_list )
    protected XRecyclerView billList;
    private BillAdapter billAdapter;

    private View userInfoView;
    protected SimpleDraweeView userIcoView;
    protected TextView userName;
    protected TextView userTitle;
    protected TextView userDepartment;
    protected TextView balanceView;

    private IUserInfoPresenter presenter;
    private String balance="0";


    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        app= (App) getActivity().getApplicationContext();
        presenter=new UserInfoPresenterCompl(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (null == mRootView) {
            mRootView = inflater.inflate(R.layout.fragment_userinfo_layout, container, false);
            ButterKnife.bind(this, mRootView);
            initViews();
            isPrepared=true;
            lazyLoad();
        }
        return mRootView;
    }

    private void initViews()
    {
        ShouerLayoutManager layoutManager=new ShouerLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        billList.setLayoutManager(layoutManager);
        billList.setLoadingMoreEnabled(false);
        billList.setPullRefreshEnabled(true);
        billList.setRefreshProgressStyle(ProgressStyle.SquareSpin);

        userInfoView=LayoutInflater.from(getContext()).inflate(R.layout.fragment_userinfo_top_layout,null,false);
        userIcoView= (SimpleDraweeView) userInfoView.findViewById(R.id.user_ico);
        userName= (TextView) userInfoView.findViewById(R.id.user_name);
        userTitle= (TextView) userInfoView.findViewById(R.id.user_title);
        userDepartment= (TextView) userInfoView.findViewById(R.id.user_department);
        balanceView= (TextView) userInfoView.findViewById(R.id.balance);

        userInfoView.findViewById(R.id.login_out).setOnClickListener(this);
        userInfoView.findViewById(R.id.qr_code).setOnClickListener(this);
        userInfoView.findViewById(R.id.modify_pass).setOnClickListener(this);
        userInfoView.findViewById(R.id.tixian_btn).setOnClickListener(this);
        billList.addHeaderView(userInfoView);
        billAdapter=new BillAdapter(getContext(),this);
        billList.setAdapter(billAdapter);
        billList.setRefreshProgressStyle(ProgressStyle.SquareSpin);
        billList.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin);
        billList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (!presenter.refresh())
                {
                    billList.refreshComplete();
                }
            }

            @Override
            public void onLoadMore() {

            }
        });

        if (null!=app.getUserDocInfo()&&TextUtils.isEmpty(app.getUserDocInfo().getDocImageUrl()))
        {
            String icoUriString="res://"+ AppUtil.getPackageName(getContext())+"/"+R.drawable.ico_userinfo_default_touxiang;
            Uri uri=Uri.parse(icoUriString);
            userIcoView.setImageURI(uri);
        }else
        {
            Uri uri=Uri.parse(FileUtil.connectFilePath(ShouerConst.RES_SERVER,app.getUserDocInfo().getDocImageUrl()));
            userIcoView.setImageURI(uri);
        }

        userName.setText(app.getUserDocInfo().getDocName());
        userTitle.setText(app.getUserDocInfo().getDocTitle());
        userDepartment.setText(app.getUserDocInfo().getDepartmentName());
        balanceView.setText(app.getUserModel().getBalance());

    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        if (billAdapter.getItemCount()==0)
        {
            ShouerLoadingDialogUtil.getInstance().show(getContext());
            presenter.refresh();
        }
    }

    @OnClick(value = {R.id.login_out})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.qr_code:
                Intent qrCodeIntent=new Intent(getContext(), QRCodeActivity.class);
                startActivity(qrCodeIntent);
                break;
            case R.id.modify_pass:
                Intent modifyIntent=new Intent(getContext(), ModifyPassActivity.class);
                startActivity(modifyIntent);
                break;
            case R.id.login_out:
                ((MainActivity)getActivity()).loginOut();
                break;
            case R.id.dialog_left_btn:
                break;
            case R.id.dialog_right_btn:
                break;
            case R.id.tixian_btn:
                if (app.isUserLogin())
                {
                    GetCashApplyDialogUtil.getInstance().show(getContext(),this,"将"+balance+"提现?");
                }
                break;
            case R.id.getcash_left_btn:
                GetCashApplyDialogUtil.getInstance().dismiss();
                break;
            case R.id.getcash_right_btn:
                GetCashApplyDialogUtil.getInstance().dismiss();
                ShouerLoadingDialogUtil.getInstance().show(getContext());
                presenter.getCashApply(balance);
                break;
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        presenter.onDestroy();
    }

    @Override
    public UserModel getUser() {
        return app.getUserModel();
    }

    @Override
    public void queryLastBillFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        billList.refreshComplete();
        Toast.makeText(getContext(),failInfo,Toast.LENGTH_SHORT).show();

    }

    @Override
    public void queryLastBillSuccess(String balance,List<BillModel> billModels) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        billList.refreshComplete();
        this.balance=balance;
        balanceView.setText(balance);
        billAdapter.refreshBillList(billModels);
    }

    @Override
    public void onMoreBillClick() {
        if (app.isUserLogin())
        {
            Intent moreBillIntent=new Intent(getContext(), BillListActivity.class);
            startActivity(moreBillIntent);
        }
    }

    @Override
    public void getCashApplySuccess() {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        presenter.queryBill();
    }

    @Override
    public void getCashApplyFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(getContext(),failInfo,Toast.LENGTH_SHORT).show();
    }
}
